/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.util;

/**
 *
 * @author J_Laptop
 */
import java.awt.event.*;

public final class InputManager implements KeyListener, MouseListener , MouseMotionListener{

    //array of key states made as integers for more possible states. 
    private int[] keys = new int[256];
    //one for each ascii character.
    private boolean[] key_state_up = new boolean[256];  //true if pressed
    private boolean[] key_state_down = new boolean[256]; //true if not pressed
    //variable that indicates when any key(s) are being pressed.
    private boolean keyPressed = false;
    //variable that indicates that some key was released this frame.
    private boolean keyReleased = false; //cleared every frame.
    //a string used as a buffer by widgets or other text input controls
    private String keyCache = "";
    //the only instantiated object
    private boolean blocked = false;
    private static InputManager instance = new InputManager();

    /**
     * Empty Constructor: nothing really needed here.
     */
    public InputManager() {
    }

    public static InputManager getInstance() {
        return instance;
    }

    public boolean isBlocked() {
        return blocked;
    }

    public void setBlocked(boolean blocked) {
        this.blocked = blocked;
    }

    public void keyPressed(KeyEvent e) {
        //System.out.println("InputManager: A key has been pressed code=" + e.getKeyCode());
        if (e.getKeyCode() >= 0 && e.getKeyCode() < 256) {
            keys[e.getKeyCode()] = (int) System.currentTimeMillis();
            key_state_down[e.getKeyCode()] = true;
            key_state_up[e.getKeyCode()] = false;
            keyPressed = true;
            keyReleased = false;
        }
    }

    public void keyReleased(KeyEvent e) {
        //System.out.println("InputManager: A key has been released code=" + e.getKeyCode());
        if (e.getKeyCode() >= 0 && e.getKeyCode() < 256) {
            keys[e.getKeyCode()] = 0;
            key_state_up[e.getKeyCode()] = true;
            key_state_down[e.getKeyCode()] = false;
            keyPressed = false;
            keyReleased = true;
        }
    }

    /**
     * This function is called if certain keys are pressed namely those used for
     * text input.
     *
     * @param e The KeyEvent fired by the awt Toolkit.
     */
    public void keyTyped(KeyEvent e) {


        if(e.getKeyChar() == '\b'){
            
            if(keyCache.length() > 0)
                keyCache = keyCache.substring(0 , keyCache.length() - 1);
            
        }
        else{
            
            keyCache += e.getKeyChar();
            
        }
   
    }

    public String getKeyCache() {
        
        return keyCache;

    }
    
    public void setKeyCache(String s){
        
        keyCache = s;
        
    }

    public void clearKeyCache() {

        keyCache = "";

    }

    /**
     * Returns true if the key (0-256) is being pressed use the KeyEvent.VK_ key
     * variables to check specific keys.
     *
     * @param key The ascii value of the keyboard key being checked
     * @return true is that key is currently being pressed.
     */
    public boolean isKeyDown(int key) {
        return key_state_down[key] && !blocked;
    }

    /**
     * Returns true if the key (0-256) is being pressed use the KeyEvent.VK_ key
     * variables to check specific keys.
     *
     * @param key The ascii value of the keyboard key being checked
     * @return true is that key is currently being pressed.
     */
    public boolean isKeyUp(int key) {
        return key_state_up[key] && !blocked;
    }

    /**
     * In case you want to know if a user is pressing a key but don't care which
     * one.
     *
     * @return true if one or more keys are currently being pressed.
     */
    public boolean isAnyKeyDown() {
        return keyPressed && !blocked;
    }

    /**
     * In case you want to know if a user released a key but don't care which
     * one.
     *
     * @return true if one or more keys have been released this frame.
     */
    public boolean isAnyKeyUp() {
        return keyReleased && !blocked;
    }

    /**
     * Only resets the key state up because you don't want keys to be showing as
     * up forever which is what will happen unless the array is cleared.
     */
    public void update() {
        //clear out the key up states
        key_state_up = new boolean[256];
        keyReleased = false;
        if (keyCache.length() > 1024) {
            System.out.println(keyCache);
            keyCache = "";
        }
    }
    MouseEvent lastClick = null;

    @Override
    public void mouseClicked(MouseEvent e) {

        lastClick = e;

    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    public MouseEvent getLastClick() {

        MouseEvent ret = lastClick;
        lastClick = null;
        return ret;

    }

    MouseEvent lastDrag = null;
    @Override
    public void mouseDragged(MouseEvent e) {
        
        lastDrag = e;
        
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        
        
        
    }
    
    public MouseEvent getLastDrag(){
        
        
        MouseEvent ret = lastDrag;
        lastDrag = null;
        return ret;
        
    }
} // InputManager
